package model

import (
	"run_order/utils"

	"gorm.io/gorm"
)

type WxUser struct {
	gorm.Model
	UserName string `json:"userName"`
	OpenId   string `json:"openId"`
	Phone    string `json:"phone"`
	Avatar   string `json:"avatar"`
	Sex      int    `json:"sex"`
}

func (table *WxUser) TableName() string {
	return "wx_user"
}

func SelectWxUserList(current int, pageSize int) []*WxUser {
	userList := []*WxUser{}
	utils.DB.Offset((current - 1) * pageSize).Limit(pageSize).Find(&userList)
	return userList
}

// 新增
func CreateWxUser(wxUser *WxUser) *gorm.DB {
	return utils.DB.Create(&wxUser)
}

// 删除
func DeleteWxUserByID(id uint) *gorm.DB {
	return utils.DB.Delete(&WxUser{}, id)
}

// 修改
func UpdateWxUser(wxUser WxUser) *gorm.DB {
	//调用save方法确认保存
	return utils.DB.Save(&wxUser)
}

func FindWxUserByOpenId(openId string) WxUser {
	wxUser := WxUser{}
	utils.DB.Where("open_id = ?", openId).First(&wxUser)
	return wxUser
}
func FindWxUserByName(userName string) WxUser {
	wxUser := WxUser{}
	utils.DB.Where("user_name = ?", userName).First(&wxUser)
	return wxUser
}
func FindWxUserByPhone(phone string) WxUser {
	wxUser := WxUser{}
	utils.DB.Where("phone = ?", phone).First(&wxUser)
	return wxUser
}
